package com.kunlun.basedata.organize.controller;

import com.kunlun.basedata.organize.model.PostModel;
import com.kunlun.basedata.organize.service.IPostService;
import com.kunlun.basedata.utils.CommonUtil;
import com.kunlun.common.model.Page;
import com.kunlun.common.utils.ResponseUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 岗位Controller类
 */
@RestController
@RequestMapping(value = "/post")
public class PostController {

    private Logger logger = LogManager.getLogger();

    @Autowired
    private IPostService postService;

    @RequestMapping(value = "/getPostList", method = RequestMethod.GET)
    public Object getPostList(PostModel postModel) {
        try {
            Map<String, Object> queryMap = CommonUtil.packageQueryMap(postModel, 0, 0);
            List<PostModel> posts = postService.getPostList(queryMap);
            return ResponseUtil.successResponse(posts);
        } catch (Exception e) {
            logger.error("PostController getPostList Error: ", e);
            return ResponseUtil.failedResponse("获取岗位数据失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/getPostPage", method = RequestMethod.GET)
    public Object getPostPage(PostModel postModel, int currentPage, int pageSize) {
        try {
            Page<PostModel> posts = postService.getPostPage(postModel, currentPage, pageSize);
            return ResponseUtil.successResponse(posts);
        } catch (Exception e) {
            logger.error("PostController getPostPage Error: ", e);
            return ResponseUtil.failedResponse("获取岗位数据失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/addPost", method = RequestMethod.POST)
    public Object addPost(PostModel postModel) {
        try {
            postService.addPost(postModel);
            return ResponseUtil.successResponse("SUCCESS");
        } catch (Exception e) {
            logger.error("PostController addPost Error: ", e);
            return ResponseUtil.failedResponse("添加岗位失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/updatePost", method = RequestMethod.POST)
    public Object updatePost(PostModel postModel) {
        try {
            postService.updatePost(postModel);
            return ResponseUtil.successResponse("SUCCESS");
        } catch (Exception e) {
            logger.error("PostController updatePost Error: ", e);
            return ResponseUtil.failedResponse("更新岗位失败！", e.getMessage());
        }
    }

    @RequestMapping(value = "/deletePost", method = RequestMethod.POST)
    public Object deletePost(List<String> list) {
        try {
            postService.deletePost(list);
            return ResponseUtil.successResponse("SUCCESS");
        } catch (Exception e) {
            logger.error("PostController deletePost Error: ", e);
            return ResponseUtil.failedResponse("删除岗位失败！", e.getMessage());
        }
    }
}
